Skip to content

chore: use path.resolve to find tree-sitter wasm files#407

Merged
Strum355 merged 1 commit into
guacsec:mainfrom
Strum355:nsc/requirements-txt-treesitter-3
Mar 11, 2026
Merged

chore: use path.resolve to find tree-sitter wasm files#407
Strum355 merged 1 commit into
guacsec:mainfrom
Strum355:nsc/requirements-txt-treesitter-3

Conversation

@Strum355

Copy link
Copy Markdown
Member

Description

The existing way seemed to be problematic for webpack when trying to integrate into https://github.com/fabric8-analytics/fabric8-analytics-vscode-extension/, this approach appears to work correctly (I've found an approach to testing integrating this library into the vscode extension that should correctly catch these kinds of issues via npm pack)

Checklist

  • I have followed this repository's contributing guidelines.
  • I will adhere to the project's code of conduct.

@Strum355
Strum355 requested a review from ruromero March 11, 2026 16:42
@qodo-code-review

Copy link
Copy Markdown

Review Summary by Qodo

Use path.resolve for tree-sitter WASM file loading

✨ Enhancement

Grey Divider

Walkthroughs

Description
• Replace CommonJS require.resolve with path.resolve for WASM file resolution
• Improve webpack compatibility by using explicit path resolution
• Remove unnecessary createRequire import and usage
Diagram
flowchart LR
  A["CommonJS require.resolve"] -->|Replace with| B["path.resolve"]
  C["createRequire import"] -->|Remove| D["path import"]
  B -->|Load WASM| E["Language.load"]
Loading

Grey Divider

File Changes

1. src/providers/requirements_parser.js ✨ Enhancement +2/-4

Replace require.resolve with path.resolve for WASM loading

• Replace createRequire from 'node:module' with path import
• Change WASM file resolution from require.resolve() to path.resolve(__dirname, ...)
• Improve webpack compatibility by using explicit file path resolution
• Simplify module initialization by removing CommonJS require wrapper

src/providers/requirements_parser.js


Grey Divider

Qodo Logo

@qodo-code-review

qodo-code-review Bot commented Mar 11, 2026

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider


Action required

1. Broken WASM resolution 🐞 Bug ✓ Correctness
Description
init() now calls Language.load(path.resolve(__dirname, ...)) in an ESM module, which can throw
ReferenceError: __dirname is not defined and prevents requirements.txt parsing from working. This
failure propagates to the pip provider because it calls getParser() during analysis.
Code

src/providers/requirements_parser.js[7]

+	return await Language.load(path.resolve(__dirname, 'tree-sitter-requirements', 'tree-sitter-requirements.wasm'));
Evidence
The package is ESM (type: module + TS emits module: esnext), so __dirname is not reliably
available; the codebase itself already guards __dirname usage elsewhere via try/catch. The pip
provider imports and executes getParser() (which calls init()), so any requirements.txt analysis
will hit the failing WASM load path.

src/providers/requirements_parser.js[1-12]
package.json[23-36]
tsconfig.json[1-25]
src/index.js[68-83]
src/providers/python_pip.js[10-15]
src/providers/python_pip.js[98-103]
src/provider.js[15-27]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`src/providers/requirements_parser.js` resolves the Tree-sitter WASM using `path.resolve(__dirname, ...)`. In this repo the package is ESM (`type: module`, TS emits `module: esnext`), so `__dirname` is not reliably available and the WASM resolution will fail, breaking requirements parsing.

### Issue Context
- `python_pip` and `python_controller` call `getParser()` which calls `init()` and thus executes the failing `Language.load(...)` path.
- The repo already contains an ESM-safe pattern for directory resolution in `src/index.js`.

### Fix Focus Areas
- src/providers/requirements_parser.js[1-12]

### Implementation direction
Update `init()` to resolve the WASM in an ESM-safe way:
- Preferred for Node runtime correctness: restore `createRequire(import.meta.url)` and `require.resolve('tree-sitter-requirements/tree-sitter-requirements.wasm')`.
- Alternative (Node ESM): use `import.meta.resolve('tree-sitter-requirements/tree-sitter-requirements.wasm')` and convert `file://` URL to a filesystem path before calling `Language.load`.
- If the intent is to load a WASM you ship within *this* package: add a build/prepack copy step to place the WASM under `dist/...` and derive the module directory via `fileURLToPath(new URL('.', import.meta.url))` (do not use `__dirname`).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

@Strum355
Strum355 enabled auto-merge (squash) March 11, 2026 16:43
Comment thread src/providers/requirements_parser.js Outdated
@Strum355
Strum355 force-pushed the nsc/requirements-txt-treesitter-3 branch from 65c0c03 to d7338b5 Compare March 11, 2026 16:54
@Strum355
Strum355 force-pushed the nsc/requirements-txt-treesitter-3 branch from d7338b5 to 28746ce Compare March 11, 2026 17:00
@Strum355
Strum355 merged commit ff266a3 into guacsec:main Mar 11, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants